class FriendLinksController < ApplicationController
  before_filter :require_admin, :except => [:new,:create]
  before_filter :find_or_initialize_friend_link,:except => [:index, :create]
  # after_filter :expire_fragment_cache, :only => [:create, :update, :destroy]
  # GET /friend_links
  # GET /friend_links.xml
  def index
    @friend_links = FriendLink.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @friend_links }
    end
  end

  # GET /friend_links/1
  # GET /friend_links/1.xml
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @friend_link }
    end
  end

  # GET /friend_links/new
  # GET /friend_links/new.xml
  def new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @friend_link }
    end
  end

  # GET /friend_links/1/edit
  def edit
  end

  # POST /friend_links
  # POST /friend_links.xml
  def create
    @friend_link = FriendLink.new(params[:friend_link])

    respond_to do |format|
      if @friend_link.save
        success_notice = (@friend_link.flag == "pass" ? "创建成功" : "感谢你的申请，我会尽快审核")
        format.html { redirect_to(@friend_link, :notice => success_notice) }
        format.xml  { render :xml => @friend_link, :status => :created, :location => @friend_link }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @friend_link.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /friend_links/1
  # PUT /friend_links/1.xml
  def update
    respond_to do |format|
      if @friend_link.update_attributes(params[:friend_link])
        format.html { redirect_to(root_url, :notice => 'FriendLink was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @friend_link.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /friend_links/1
  # DELETE /friend_links/1.xml
  def destroy
    @friend_link.destroy

    respond_to do |format|
      format.html { redirect_to(friend_links_url) }
      format.xml  { head :ok }
    end
  end

  private
  def find_or_initialize_friend_link
    @friend_link = params[:id] ? FriendLink.find(params[:id]) : FriendLink.new
  end
  
  # => 清除右侧目录树片段的缓存
  def expire_fragment_cache
    expire_fragment(RecordSweeper::PATH.merge(:action_suffix => 'friend_links'))
  end
  
end
